package com.example.rbq

import org.junit.Test

class Test {

    @Test
    fun test(){
        val input0 = "babad"
        val input1 = "cbbd"
        val input2 = ""
        val input3 = "bb"
        val input4 = "aba"
        val input5 = "babad"
        val input6 = "caba"
        val input7 = "ccc"

        println(longestPalindrome(input0))
        println(longestPalindrome(input1))
        println(longestPalindrome(input2))
        println(longestPalindrome(input3))
        println(longestPalindrome(input4))
        println(longestPalindrome(input5))
        println(longestPalindrome(input6))
        println(longestPalindrome(input7))
    }

    fun longestPalindrome(s: String): String {
        val n = s.length
        if(n == 0) return ""
        if(n == 1) return s[0].toString()
        // 回文串长度
        var ans = s[0].toString()
        var ansLen = 1
        for(i in 0 until n - 1) {
            for(j in i+1 until n){
                val step = j + ansLen
                if(step > n) break
                val tempLen = step - i +1
                if (tempLen < ansLen) continue
                val str = s.substring(i,step)
//                println("**i:$i,step:$step,str:$str**")
                if(str.reversed() == str){
                    ans = str
                }
            }
            ansLen = ans.length
        }
        return ans
    }
}